package org.uni_hi.lsep.gui;

import java.awt.Component;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

import org.uni_hi.lsep.gui.util.Config;
import org.uni_hi.lsep.text.ChapterNode;

public class NavigationTreeCellRenderer extends DefaultTreeCellRenderer
implements TreeCellRenderer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3557730791158843237L;
	private ImageIcon _testLeafIcon,_testNodeIcon,_testNodeExpandedIcon,_nodeIcon,_nodeExpandedIcon,_leafIcon;

	public NavigationTreeCellRenderer(){
		try {
			_testLeafIcon=new ImageIcon(new ImageIcon(Config.testLeafIcon).getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		try {
			_testNodeIcon=new ImageIcon(new ImageIcon(Config.testNodeIcon).getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		try {
			_testNodeExpandedIcon=new ImageIcon(new ImageIcon(Config.testNodeExpandedIcon).getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		try {
			_nodeIcon=new ImageIcon(new ImageIcon(Config.nodeIcon).getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));
		} catch (NullPointerException e) {
			e.printStackTrace();
		}

		try {
			_nodeExpandedIcon=new ImageIcon(new ImageIcon(Config.nodeExpandedIcon).getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));
		} catch (NullPointerException e) {
			e.printStackTrace();
		}

		try {
			_leafIcon=new ImageIcon(new ImageIcon(Config.leafIcon).getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));
		} catch (NullPointerException e) {
			e.printStackTrace();
		}

	}

	public Component getTreeCellRendererComponent(
			JTree tree,
			Object value,
			boolean sel,
			boolean expanded,
			boolean leaf,
			int row,
			boolean hasFocus) {

		super.getTreeCellRendererComponent(
				tree, value, sel,
				expanded, leaf, row,
				hasFocus);
		ChapterNode cn=(ChapterNode)((DefaultMutableTreeNode)value).getUserObject();
		if (leaf) {
			if(cn.isTest()){
				setIcon(_testLeafIcon);
				setToolTipText("Dies ist ein Test.");
			}else{
				setIcon(_leafIcon);
			}
		}else {
			if(expanded){
				if(cn.isTest()){
					setIcon(_testNodeExpandedIcon);
				}else{
					setIcon(_nodeExpandedIcon);
				}
			}else{
				if(cn.isTest()){
					setIcon(_testNodeIcon);
				}else{
					setIcon(_nodeIcon);
				}
			}
			setToolTipText(null); //no tool tip
		}

		return this;
	}


}
